package com.jsgt.greekfire.dao.dao

import android.text.Selection
import androidx.lifecycle.LiveData
import androidx.room.*
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.model.WalletBean

@Dao
interface WalletBeanDao {
    @Query("select * from wallet")
    fun get(): LiveData<List<WalletBean>>

    @Query("select * from wallet where id = (:id)")
    fun get(id: Int): LiveData<WalletBean>

    @Query("select * from wallet where `selection` = (:selection)")
    fun getBySelection(selection: Int): LiveData<List<WalletBean>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun add(walletBean: WalletBean)

    @Delete
    fun delete(walletBean: WalletBean?)

    @Update
    fun update(walletBean: WalletBean)

    @Update(entity = WalletBean::class)
    fun updateName(vararg name: WalletBean.Name)

    @Query("update wallet set `selection` = 0 ")
    fun cleanSelection()

    @Query("update wallet set `selection` = :selection where `id` = :id")
    fun updateSelection(selection: Int, id: Int)

    //使用内连接查询
    @Query("SELECT identity.name as identityName, identity.backup as isBackup, identity.portrait, wallet.id, wallet.`key`, wallet.identity_id as identityId, wallet.name as walletName, wallet.address,wallet.selection  from identity INNER JOIN wallet ON identity.id = wallet.identity_id Order by wallet.identity_id")
    fun getIdentityWallet(): LiveData<List<IdentityWallet?>>

    @Query("SELECT identity.name as identityName, identity.backup as isBackup, identity.portrait, wallet.id, wallet.`key`, wallet.identity_id as identityId, wallet.name as walletName, wallet.address,wallet.selection  from identity INNER JOIN wallet ON identity.id = wallet.identity_id where wallet.`id` = :id")
    fun getIdentityWallet(id: Int): LiveData<IdentityWallet>

    @Query("SELECT identity.name as identityName, identity.backup as isBackup, identity.portrait, wallet.id, wallet.`key`, wallet.identity_id as identityId, wallet.name as walletName, wallet.address,wallet.selection  from identity INNER JOIN wallet ON identity.id = wallet.identity_id where wallet.`selection` = :selection")
    fun getIdentityWalletBySelection(selection: Int): LiveData<List<IdentityWallet>>
}